package com.buka.goods.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.buka.goods.mapper.ClassifyMapper;
import com.buka.goods.service.ClassifyService;
import com.buka.server.pojo.Classify;
import com.itbuka.feign.MinioFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class ClassifyServiceImpl implements ClassifyService {

    @Autowired
     private ClassifyMapper classifyMapper;
    @Autowired
    private MinioFeign minioFeign;
    @Override
    public List<Classify> findAllFirst() {
        Example example=new Example(Classify.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete",0);
        criteria.andEqualTo("superiorId",0);
        return classifyMapper.selectByExample(example);
    }

    @Override
    public List<Classify> findAllByFirstId(Long id) {
        Example example=new Example(Classify.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete",0);
        criteria.andEqualTo("superiorId",id);
        return classifyMapper.selectByExample(example);
    }

    @Override
    public int AddFirstGoods(Classify classify) {

        long id= IdWorker.getId();
        classify.setSuperiorId(0L);
        classify.setId(id);
        return classifyMapper.insertSelective(classify);

    }

    @Override
    public int AddNextGoods(Classify classify) {
        long id= IdWorker.getId();
        classify.setId(id);
        Example example=new Example(Classify.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("superiorId", classify.getSuperiorId());

        return classifyMapper.insertSelective(classify);
    }

    @Override
    public int updateGoods(Classify classify) {
        return classifyMapper.updateByPrimaryKeySelective(classify);
    }

    @Override
    public int deleteGoods(Integer id) {

        Example example=new Example(Classify.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        Classify classify =new Classify();
        classify.setIsDelete(1);
        int a= classifyMapper.updateByExampleSelective(classify,example);
        return a;
    }

    @Override
    public int addAndFile(String classify, MultipartFile file) {
        Classify classify1=new Classify();
        String url = minioFeign.uploadF(file).getData();
        if (url.equals("1") || url.equals("2")) {
            return -1;
        }
        classify1.setClassifyLogo(url);
        classify1.setId(IdWorker.getId());
        classify1.setClassify(classify);
        return classifyMapper.insertSelective(classify1);
    }
}
